from PyQt5.QtWidgets import QLabel, QPushButton, QVBoxLayout, QWidget, QHBoxLayout

from utils.delLayout import delLayout


class TopBarParams:
    def __init__(self, newListener, aboutListener, resetListener, lruListener, fifoListener,settingListener):
        self.newListener = newListener
        self.aboutListener = aboutListener
        self.resetListener = resetListener
        self.lruListener = lruListener
        self.fifoListener = fifoListener
        self.settingListener = settingListener


class UI_Main:
    def __init__(self):
        self.page_list = None  # 页面请求队列
        self.current_page = None  # 左侧窗口
        self.out_put = None  # 右侧窗口
        self.page_show = None  # 显示当前下标

    def refreshIndex(self, index):
        self.page_show.setText("当前请求下标：{}".format(index - 1))

    def refreshCurrent(self, current_pages):
        delLayout(self.current_page)
        for item in current_pages:
            obj_vg = QWidget()
            obj_vg.setStyleSheet("""
                background-color: #B2DFDB;
                """)

            obj = QPushButton("{}".format(item))
            obj_container = QVBoxLayout()
            obj_container.addWidget(obj)
            obj_vg.setLayout(obj_container)
            self.current_page.addWidget(obj_vg)

        self.current_page.addStretch()
        pass

    def refreshOutput(self, out_str):
        delLayout(self.out_put)
        out_put = QLabel(out_str)
        self.out_put.addWidget(out_put)

    def refreshPages(self, list_input):
        delLayout(self.page_list)
        for item in list_input:
            obj_vg = QWidget()
            obj_vg.setStyleSheet("""
                background-color: #B2DFDB;
                """)

            obj = QPushButton("{}".format(item))
            obj_container = QVBoxLayout()
            obj_container.addWidget(obj)
            obj_vg.setLayout(obj_container)
            self.page_list.addWidget(obj_vg)
        self.page_list.addStretch()

    def createTopBar(self, top_bar_Params):
        container_widget = QWidget()
        container_widget.setStyleSheet("background-color: #26A69A;")

        top_bar = QHBoxLayout()

        newButton = QPushButton("生成随机")
        newButton.setStyleSheet("background-color: #3f50b5;")
        settingButton = QPushButton("设置")
        settingButton.setStyleSheet("background-color: #3f50b5;")

        aboutButton = QPushButton("关于")
        aboutButton.setStyleSheet("background-color: #3f50b5;")
        resetButton = QPushButton("重置")
        resetButton.setStyleSheet("background-color: #3f50b5;")

        lruButton = QPushButton("LRU")
        lruButton.setStyleSheet("background-color: #00897B;")
        fifoButton = QPushButton("FIFO")
        fifoButton.setStyleSheet("background-color: #00897B;")

        pageLabel = QLabel("当前请求下标：未开始")
        self.page_show = pageLabel

        newButton.clicked.connect(lambda: top_bar_Params.newListener(self))
        aboutButton.clicked.connect(top_bar_Params.aboutListener)
        resetButton.clicked.connect(lambda: top_bar_Params.resetListener(self))
        lruButton.clicked.connect(
            lambda: top_bar_Params.lruListener(self))
        fifoButton.clicked.connect(
            lambda: top_bar_Params.fifoListener(self))
        settingButton.clicked.connect(
            lambda: top_bar_Params.settingListener(self))
        top_bar.addWidget(newButton)
        top_bar.addWidget(settingButton)
        top_bar.addWidget(aboutButton)
        top_bar.addWidget(resetButton)
        top_bar.addSpacing(40)
        top_bar.addWidget(lruButton)
        top_bar.addWidget(fifoButton)
        top_bar.addWidget(pageLabel)
        top_bar.addStretch()

        container_widget.setLayout(top_bar)
        return container_widget

    def createPageList(self):
        page_list_vg = QWidget()
        page_list_vg.setStyleSheet("""
                            background-color: #E0F2F1;
                            border-top-left-radius:10px;
                            border-top-right-radius:10px;
                            border-bottom-left-radius:10px;
                            border-bottom-right-radius:10px;
                            padding:2px 4px;
                            """)
        self.page_list = QHBoxLayout()
        page_list_vg.setLayout(self.page_list)
        return page_list_vg

    def createChart(self):
        chart_vg = QHBoxLayout()

        wait_cg_container = QWidget()
        run_cg_container = QWidget()

        wait_cg_container.setStyleSheet("""
                background-color: #B2DFDB;
                border:4px solid #757de8; 
                border-top-left-radius:10px;
                border-top-right-radius:10px;
                border-bottom-left-radius:10px;
                border-bottom-right-radius:10px;
                padding:2px 4px;
                """)
        run_cg_container.setStyleSheet("""
                background-color: #B2DFDB;
                border:4px solid #757de8; 
                border-top-left-radius:10px;
                border-top-right-radius:10px;
                border-bottom-left-radius:10px;
                border-bottom-right-radius:10px;
                padding:2px 4px;
                """)

        wait_cg_container.setFixedHeight(300)
        run_cg_container.setFixedHeight(300)

        wait_cg = QVBoxLayout()
        run_cg = QVBoxLayout()

        chart_vg.addLayout(wait_cg)
        chart_vg.addLayout(run_cg)

        ################################
        text_queue_wait = QLabel("当前页面")

        wait_cg.addWidget(text_queue_wait)
        wait_cg.addWidget(wait_cg_container)

        wait_list = QVBoxLayout()
        wait_cg_container.setLayout(wait_list)
        self.current_page = wait_list

        ################################
        ################################
        text_queue_run = QLabel("输出")

        run_cg.addWidget(text_queue_run)
        run_cg.addWidget(run_cg_container)

        run_list = QVBoxLayout()
        run_cg_container.setLayout(run_list)
        self.out_put = run_list

        return chart_vg

    def CreateMainWindow(self, screen, top_bar_params):
        win = QWidget()
        win.setWindowTitle("页面调度")
        width = 800
        height = 600
        win.setGeometry((screen.width - width) / 2, (screen.height - height) / 2, width, height)

        outline_vg = QVBoxLayout()

        top_bar = self.createTopBar(top_bar_params)
        page_list = self.createPageList()
        chart = self.createChart()

        outline_vg.addWidget(top_bar)

        outline_vg.addStretch(1)

        outline_vg.addWidget(QLabel("请求队列: "))
        outline_vg.addWidget(page_list)

        outline_vg.addLayout(chart)

        outline_vg.addStretch(30)

        win.setLayout(outline_vg)

        return win
